﻿using HrEFCore;
using HrIService;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HrService
{
	public class EIService : IEIService
	{
		private readonly MyDbContext myDbContext;

		public EIService(MyDbContext myDbConext)
		{
			this.myDbContext = myDbConext;
		}

		public async Task<int> AddEI(EI ei)
		{
			myDbContext.EI.AddAsync(ei);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> Revise(short resId, DateTime registTime, string EIChecker)
		{
			//根据resId修改EI中的RegistTime和EIChecker数据 RegisteTime
			var sql = $"UPDATE [dbo].[EI] SET RegisteTime = '{registTime}',Checker='{EIChecker}' WHERE ResumeId = '{resId}'";
			return await myDbContext.Database.ExecuteSqlRawAsync(sql);
		}

		public async Task<int> ReviseInterviewStatus(short resId, short interviewStatus)
		{
			////根据resId修改EI中的InterviewStatus数据 
			var sql = $"UPDATE [dbo].[ER] SET InterviewStatus = '{interviewStatus}' WHERE [ResId] = '{resId}'";
			return await myDbContext.Database.ExecuteSqlRawAsync(sql);
		}

		public async Task<int> UpdateEIByResumeId(EI updatedEI)
		{
			var existingEI = await myDbContext.EI.FirstOrDefaultAsync(e => e.ResumeId == updatedEI.ResumeId);
			if (existingEI != null)
			{
				// 更新 EI 对象的属性值
				existingEI.HumanName = updatedEI.HumanName;
				existingEI.InterviewAmount = updatedEI.InterviewAmount;
				existingEI.HumanMajorKindId = updatedEI.HumanMajorKindId;
				existingEI.HumanMajorKindName = updatedEI.HumanMajorKindName;
				existingEI.HumanMajorId = updatedEI.HumanMajorId;
				existingEI.HunmaMajorName = updatedEI.HunmaMajorName;
				existingEI.ImageDegree = updatedEI.ImageDegree;
				existingEI.NativeLanguageDegree = updatedEI.NativeLanguageDegree;
				existingEI.ForeignLanguageDegree = updatedEI.ForeignLanguageDegree;
				existingEI.ResponseSpeedDegree = updatedEI.ResponseSpeedDegree;
				existingEI.EQDegree = updatedEI.EQDegree;
				existingEI.IQDegree = updatedEI.IQDegree;
				existingEI.MultiQualityDegree = updatedEI.MultiQualityDegree;
				existingEI.Register = updatedEI.Register;
				existingEI.RegisteTime = updatedEI.RegisteTime;
				existingEI.Result = updatedEI.Result;
				existingEI.InterviewComment = updatedEI.InterviewComment;
				existingEI.InterviewStatus = updatedEI.InterviewStatus;
				existingEI.CheckComment = updatedEI.CheckComment;
				// 保存更改到数据库
				myDbContext.EI.Update(existingEI);
				return await myDbContext.SaveChangesAsync();
			}
			else
			{
				return 0; // 如果找不到匹配的 EI 对象，返回 0 表示没有进行任何更新
			}
		}
	}
}
